from web3 import Web3, HTTPProvider
import json

# 钱包地址
address = '0xb9d84D86c0CcA53aF899bF583367a738b9Ef8559'
# rpc节点
rpc = 'https://bsc-dataseed1.binance.org:443'
# 项目合约地址 whalefall
token_address = '0x587c16b84c64751760f6e3e7e32f896634704352'

web3 = Web3(HTTPProvider(rpc))

with open('/Users/dingzhiyao/Desktop/solidity_pts/web3py/whale_abi.json') as file:
    token_abi = file.read()

token_abi = json.loads(token_abi)
token_address = web3.toChecksumAddress(token_address)

def get_base_token_balance(wallet_address):
    balance = web3.fromWei(web3.eth.getBalance(wallet_address), "ether")
    print(f"{rpc} 上的基础代币余额为 {balance}")

def get_other_token_balance(token_address, token_abi, wallet_address):
    # 实例化合约
    token_contract = web3.eth.contract(address=token_address, abi=token_abi)

    # 待优化，应该根据代币的精度来确定fromWei方法使用的单位
    token_balance = web3.fromWei(token_contract.functions.balanceOf(wallet_address).call(), 'ether')
    token_name = token_contract.functions.name().call()
    print(f"{wallet_address} 上 {token_name} 的余额为 {token_balance}")

get_base_token_balance(address)

get_other_token_balance(token_address, token_abi, address)
